<?php
/**
 * Restore handling for resourcepage.
 *
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package package_name
 *//** */


//This function executes all the restore procedure about this mod
function refworks_restore_mods($mod,$restore) {

    global $CFG;

    $status = true;

    //Get record from backup_ids
    $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);

    if ($data) {
        //Now get completed xmlized object
        $info = $data->info;
        //if necessary, write to restorelog and adjust date/time fields
        if ($restore->course_startdateoffset) {
            restore_log_date_changes('refworks', $restore, $info['MOD']['#'], array('TIMEMODIFIED'));
        }

        //Now, build the wiki record structure
        $wiki->course = $restore->course_id;
        $wiki->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $wiki->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $wiki->autopopfolder = backup_todb($info['MOD']['#']['AUTOPOPFOLDER']['0']['#']);
        $wiki->autopopdata = backup_todb($info['MOD']['#']['AUTOPOPDATA']['0']['#']);

        //The structure is equal to the db, so insert the wiki
        $newid = insert_record ("refworks",$wiki);

        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>".get_string("modulename","refworks")." \"".format_string(stripslashes($wiki->name),true)."\"</li>";
        }
        backup_flush(300);

        if ($newid) {
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code,$mod->modtype,
            $mod->id, $newid);
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }

    return $status;
}

//Return a content decoded to support interactivities linking. Every module
//should have its own. They are called automatically from
//resourcepage_decode_content_links_caller() function in each module
//in the restore process
function refworks_decode_content_links ($content,$restore) {
    global $CFG;

    $result = $content;

    //Link to the list of resourcepages
    $searchstring='/\$@(REFWORKSINDEX)\*([0-9]+)@\$/';
    //We look for it
    $foundset=array();
    preg_match_all($searchstring,$content,$foundset);
    //If found, then we are going to look for its new id (in backup tables)
    if ($foundset[0]) {
        //print_object($foundset);                                     //Debug
        //Iterate over foundset[2]. They are the old_ids
        foreach($foundset[2] as $old_id) {
            //We get the needed variables here (course id)
            $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
            //Personalize the searchstring
            $searchstring='/\$@(REFWORKSINDEX)\*('.$old_id.')@\$/';
            //If it is a link to this course, update the link to its new location
            if(isset($rec->new_id)) {
                //Now replace it
                $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/refworks/index.php?id='.$rec->new_id,$result);
            } else {
                //It's a foreign link so leave it as original
                $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/refworks/index.php?id='.$old_id,$result);
            }
        }
    }

    //Link to resourcepage view by moduleid
    $searchstring='/\$@(REFWORKSVIEWBYID)\*([0-9]+)@\$/';
    //We look for it
    preg_match_all($searchstring,$result,$foundset);
    //If found, then we are going to look for its new id (in backup tables)
    if ($foundset[0]) {
        //print_object($foundset);                                     //Debug
        //Iterate over foundset[2]. They are the old_ids
        foreach($foundset[2] as $old_id) {
            //We get the needed variables here (course_modules id)
            $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
            //Personalize the searchstring
            $searchstring='/\$@(REFWORKSVIEWBYID)\*('.$old_id.')@\$/';
            //If it is a link to this course, update the link to its new location
            if(isset($rec->new_id)) {
                //Now replace it
                $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/refworks/view.php?id='.$rec->new_id,$result);
            } else {
                //It's a foreign link so leave it as original
                $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/refworks/view.php?id='.$old_id,$result);
            }
        }
    }

    return $result;
}

//This function makes all the necessary calls to xxxx_decode_content_links()
//function in each module, passing them the desired contents to be decoded
//from backup format to destination site/course in order to mantain inter-activities
//working in the backup/restore process. It's called from restore_decode_content_links()
//function in restore process
function refworks_decode_content_links_caller($restore) {
    //TODO - do we ever need to call refworks_decode_content_links?
    /*try {
        $newid=module_resourcepage::$inst->restore_decode_links($restore);
        return true;
    } catch(Exception $e) {
        return false;
    }*/
    return true;
}
/*
//This function returns a log record with all the necessay transformations
//done. It's used by restore_log_module() to restore modules log.
function resourcepage_restore_logs($restore,$log) {
    try {
        return module_resourcepage::$inst->restore_log_entry($restore,$log);
    } catch(Exception $e) {
        return false;
    }
}
*/
?>
